package edu.school.controller;

import com.github.pagehelper.PageInfo;
import edu.school.entity.*;
import edu.school.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class ClientController {
    @Autowired
    GoodsService gs;
    @Autowired
    BoardsService bs;
    @Autowired
    TieziService ts;
    @Autowired
    AdoptService as;
    @RequestMapping("/client/main")
    public String toHome(){
        return "client/main";
    }
    @RequestMapping("/main/main")
    public String toHome1(){
        return "common/main";
    }

    //失物大厅
    @RequestMapping(value = "/client/swdt",method = {RequestMethod.GET,RequestMethod.POST})
    public String pageList(Goods record, HttpSession session,
                           @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                           ModelMap modelMap){

        PageInfo<Goods> pageInfo = gs.getQtPageList(pageNum,pageSize,record);
        record.setStatus(String.valueOf(0));
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("record",record);
        return "/client/goods";
    }
    //公告
    @RequestMapping(value = "/client/board", method = {RequestMethod.GET, RequestMethod.POST})
    public String boardPageList(Board record, HttpSession session,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                ModelMap modelMap) {
        PageInfo<Board> pageInfo = bs.getPageList(pageNum, pageSize, record);
        modelMap.addAttribute("pageInfo", pageInfo);
        modelMap.addAttribute("record", record);
        return "/client/boards";
    }
    //论坛
    @RequestMapping(value = "/client/tiezi", method = {RequestMethod.GET, RequestMethod.POST})
    public String boardPageList(Tiezi record, HttpSession session,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                ModelMap modelMap) {
        PageInfo<Tiezi> pageInfo = ts.getPageList(pageNum, pageSize, record);
        modelMap.addAttribute("pageInfo", pageInfo);
        modelMap.addAttribute("record", record);
        modelMap.addAttribute("url","tiezi");
        return "/client/tiezi";
    }
    //前台帖子管理
    @RequestMapping(value = "/client/mytiezi",method = {RequestMethod.GET,RequestMethod.POST})
    public String myTiezi(Tiezi record,HttpSession session,
                          @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                          @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                          ModelMap modelMap){
        User user= (User) session.getAttribute("user");
        if(user!=null){
            record.setUser_id(user.getId());
        }
        PageInfo<Tiezi> pageInfo = ts.getPageList(pageNum,pageSize,record);
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("record",record);
        modelMap.addAttribute("url","tiezi");
        return "/client/tiezi_list";
    }
    // 前台认领列表
    @RequestMapping(value = "/client/adopt", method = {RequestMethod.GET, RequestMethod.POST})
    public String adoptList(Adopt record, HttpSession session,
                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                            ModelMap modelMap) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            record.setU_id(user.getId());
        }
        PageInfo<Adopt> pageInfo = as.getPageList(pageNum, pageSize, record);
        modelMap.addAttribute("pageInfo", pageInfo);
        modelMap.addAttribute("record", record);
        modelMap.addAttribute("url", "adopt");
        return "/client/adopt_list";
    }
    //用户退出
    @RequestMapping("/client/loginout")
    public String loginout(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/client/main";
    }

}
